Source: models/community-set.js

/**
 * @auther Yuxin Ma
 * @module models/community-set
 * @exports CommunitySet
 */

'use strict';
var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone');
Backbone.$ = $;
//require('backbone-relational');

var Node = require('./node-model');
var Edge = require('./edge-model');
var NodeCollection = require('../collections/node-collection');
var EdgeCollection = require('../collections/edge-collection');
var Graph = require('./graph-model');
var Filter = require('./query-filter');
var FilterCollection = require('../collections/filter-collection');
var Community = require('./community');
var CommunityCollection = require('../collections/community-collection');
var commLoader = require('./demodata/comm-loader');
var commLoader1123 = require('./demodata/comm-loader-1123');


var CommunityLink = Backbone.Model.extend({

});

var CommunityLinkCollection = Backbone.Collection.extend({
    model: CommunityLink
});


var CommunitySet = Backbone.Model.extend( /** @lends models/CommunitySet.prototype */ {

    /**
     * 整体社团结果,其中包含多个社团元素Community。
     * @augments Backbone.Model
     * @constructs
     */

    defaults: {
        lastUpdateDate: null
    },

    initialize: function() {
        /**
         * TODO: 2015.11.19 先使用方舟生成的community.json用纯前端方式填充,接入后端后删除。
         */
    },

    parse: function(data, options) {

        if (data.DEBUG_COMM_FRONTEND === true) {
            this.set('DEBUG_COMM_FRONTEND', true);

            return this.__loadDemoCommunityJSON();
        }
    },

    /**
     * 前端生成的测试数据加载方法。
     * @returns {{communityProperties: string[], communities: *, communityLinks: *, boundaryGraph: *}}
     * @private
     */
    __loadDemoCommunityJSON: function() {
        var demoData = commLoader.loadCommunityJSON();

        // 解析community属性部分
        var communities = new CommunityCollection();

        demoData.community.forEach(function(d) {
            var community = new Community({
                id: d.id,
                size: d.size,
                neighbors: new CommunityLinkCollection(),
                boundaryNodes: new NodeCollection(),
                graph: new Graph()
            });

            communities.add(community);
        });

        // 解析community连接部分
        var links = new EdgeCollection();

        demoData.link.forEach(function(d) {
            var sourceComm = communities.get(d.source);
            var targetComm = communities.get(d.target);
            var sourceEdgeCol = sourceComm.get('neighbors');
            var targetEdgeCol = targetComm.get('neighbors');

            var e = new Edge({
                source: sourceComm,
                target: targetComm,
                weight: d.weight,
                direction: Graph.Direction.OUT
            });

            links.add(e);

            sourceEdgeCol.add(new CommunityLink({
                edge: e,
                direction: Graph.Direction.OUT
            }));

            targetEdgeCol.add(new CommunityLink({
                edge: e,
                direction: Graph.Direction.IN
            }));
        });

        // 解析边界节点,做成图结构。
        var boundaryGraph = new Graph();

        demoData.boundary.nodes.forEach(function(d) {
            var correspondingCommunity = communities.get(d.comID);

            var n = new Node({
                id: d.id,
                keyFlag: d.keyFlag,
                gender: d.gender,
                name: d.name,
                community: correspondingCommunity
            });

            correspondingCommunity.get('boundaryNodes').add(n);
            boundaryGraph.addNode(n);
        });

        demoData.boundary.edges.forEach(function(d) {
            var e = new Edge({
                source: boundaryGraph.getNodeByProperty({name: d[0]}),
                target: boundaryGraph.getNodeByProperty({name: d[1]})
            });

            boundaryGraph.addEdge(e);
        });

        return {
            communityProperties: ['id', 'size'],
            communities: communities,
            communityLinks: links,
            boundaryGraph: boundaryGraph,
            lastUpdateDate: new Date()
        };
    },

    __loadDemoCommunityJSON1123: function() {

        var demoData = commLoader1123.loadCommunityJSON();

        // 解析community属性部分
        var communities = new CommunityCollection();

        return {
            communityProperties: ['id', 'size'],
            communities: communities,
            communityLinks: links,
            boundaryGraph: boundaryGraph,
            lastUpdateDate: new Date()
        };
    },

    /**
     * 获得所有Community的数组。可使用filter对象进行community的属性过滤。最好不要直接访问CommuinitySet的communities成员,因为该方法会解决前端删除等一些缓存操作的适配问题。
     * @param {*} filterOptions - 过滤设置
     * @returns {Community[]} 所有Community的数组
     */
    getCommunityArray: function(filterOptions) {

        var communities = this.get('communities');

        if (filterOptions === undefined) {
            return communities.filter({'__deleted': false});
        } else {
            filterOptions.__deleted = false;
            return communities.filter(filterOptions);
        }
    },

    /**
     * 获得Community的个数。
     * @returns {number} 该CommunitySet中Community的个数
     */
    getNumberOfCommunities: function() {
        return this.get('communities').length;
    },

    /**
     * 重新计算Community的布局,会直接修改内部每个Community的视觉属性。
     * @param options 布局参数设置
     */
    layout: function(options) {
        throw new Error('not implemented');
    }
});


module.exports = CommunitySet;